﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using CNVP.Framework.Utils;
using CNVP.Framework.Helper;
using System.Xml;
using CNVP.Config;

namespace CNVP.JShop.BLL
{
    public class LabelCode
    {
        private string FileName = HttpContext.Current.Server.MapPath(UIConfig.InstallDir+UIConfig.AdminDir+"/Template/LabelCode.config");
        public LabelCode()
        {
            if (!FileUtils.FileExists(FileName))
            {
                LogHelper.Write("配置文件读取失败", "请检查LabelCode.config。");
            }
        }
        /// <summary>
        /// 读取属性
        /// </summary>
        /// <returns></returns>
        public DataTable GetProperty()
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(FileName);
            XmlNodeList RootList = Xml.SelectNodes("cnvp/unit");

            DataTable _Dt = new DataTable("Result");
            _Dt.Columns.Add("Type", typeof(string));
            _Dt.Columns.Add("Name", typeof(string));
            _Dt.Columns.Add("Script", typeof(string));

            foreach (XmlNode Xn in RootList)
            {
                DataRow _Rows = _Dt.NewRow();
                _Rows["Type"] = Xn.ChildNodes[0].InnerText;
                _Rows["Name"] = Xn.ChildNodes[1].InnerText;
                _Rows["Script"] = Xn.ChildNodes[2].InnerText;
                _Dt.Rows.Add(_Rows);
            }
            return _Dt;
        }
        /// <summary>
        /// 添加单元
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="Name"></param>
        /// <param name="Script"></param>
        public void LabelCodeAdd(string Type,string Name,string Script)
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(FileName);

            XmlNode Root = Xml.SelectSingleNode("cnvp");
            XmlElement Xe1 = Xml.CreateElement("unit");

            XmlElement XeSub1 = Xml.CreateElement("type");
            XeSub1.InnerText = Type;
            Xe1.AppendChild(XeSub1);
            XmlElement XeSub2 = Xml.CreateElement("name");
            XeSub2.InnerText = Name;
            Xe1.AppendChild(XeSub2);
            XmlElement XeSub3 = Xml.CreateElement("script");
            XeSub3.InnerText = Script;
            Xe1.AppendChild(XeSub3);

            Root.AppendChild(Xe1);
            Xml.Save(FileName);
        }
    }
}